package com.dz51.core.basedao.query.impl;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.dz51.core.basedao.dao.IConditionSupport.QueryStringType;
import com.dz51.core.basedao.query.IConditionBuilder;
import com.dz51.core.page.Pager;

/**
 * @Title: DefaultQueryConditionBuilder.java
 * @Package com.dz51.core.basedao.query.impl
 * @Description: 查询构造器默认实现
 * @author Zhaoqt
 * @date 2014-5-4 上午9:15:47
 * @version V1.0
 */
public class DefaultQueryConditionBuilder implements IConditionBuilder {

	private String queryString; // 默认查询语句
	private QueryStringType queryStringType; // 查询语言类型 SQL,HQL
	private String property;// 查找属性名称
	private Map<String, Object> propertyMap = new LinkedHashMap<String, Object>();// 查找多重属性map[KEY:property_name,VALUE:property_value]
	private Map<String, OrderType> sortMap = new LinkedHashMap<String, OrderType>();
	public static final String PROPERTY_NAME_KEY = "prop_name";// 多重属性名称
	public static final String PROPERTY_KEYWORD_VALUE = "prop_keyword";// 多重属性关键词内容
	private String keyword;// 查找关键字
	private Criteria criteria;
	private boolean isCriteria = true;
	private Pager pager;

	/**
	 * 初始化基础查询语句
	 * 
	 * @Title: setBaseQueryString
	 * @param paramString
	 */
	@Override
	public void setBaseQueryString(String queryString) {
		// TODO Auto-generated method stub

	}

	/**
	 * 返回对象
	 * 
	 * @Title: setResultObject
	 * @param resultObject
	 */
	@Override
	public void setResultObject(Class<?> resultObject) {
		// TODO Auto-generated method stub

	}

	/**
	 * 查询语言类型 SQL,HQL
	 * 
	 * @Title: setQueryStringType
	 * @param queryStringType
	 */
	@Override
	public void setQueryStringType(QueryStringType queryStringType) {
		// TODO Auto-generated method stub

	}

	/**
	 * 添加查询条件
	 * 
	 * @Title: addCondition
	 * @param name
	 * @param object
	 */
	@Override
	public void addCondition(String name, Object object) {
		propertyMap.put(name, object);
	}

	/**
	 * 添加排序
	 * 
	 * @Title: addSort
	 * @param orderType
	 * @param sort
	 */
	@Override
	public void addSort(OrderType orderType, String sort) {
		this.sortMap.put(sort, orderType);
	}

	/**
	 * 分页计算
	 * 
	 * @Title: addPager
	 * @param pager
	 */
	@Override
	public void addPager(Pager pager) {
		this.pager = pager;
	}

	@Override
	public void isCriteria(boolean is) {
		this.isCriteria = is;
	}

	@Override
	public void setKeyword(String keyword, String property) {
		this.setKeyword(keyword);
		this.setProperty(property);
	}

	/**
	 * 排序Criteria
	 * 
	 * @Title: addCriteriaOrder
	 */
	public void addCriteriaOrder() {

		if (this.sortMap.size() > 0) {
			for (Iterator<Entry<String, OrderType>> ite = sortMap.entrySet().iterator(); ite.hasNext();) {
				Entry<String, OrderType> entry = ite.next();
				String sort = entry.getKey();
				OrderType orderType = entry.getValue();
				if (OrderType.ASC.equals(orderType)) {
					criteria.addOrder(Order.asc(sort));
				} else {
					criteria.addOrder(Order.desc(sort));
				}
			}
		}
	}

	/**
	 * 分页查询
	 * 
	 * @Title: addCriteriaPager
	 */
	public void addCriteriaPager() {
		if (pager != null) {
			Integer pageNumber = pager.getPageNumber();
			Integer pageSize = pager.getPageSize();
			criteria.setFirstResult((pageNumber - 1) * pageSize);
			criteria.setMaxResults(pageSize);
		}

	}

	/**
	 * 查找关键字, 属性名称
	 * 
	 * @Title: addCriteriaKeyword
	 * @param keyword
	 */
	public void addCriteriaKeyword() {
		if (StringUtils.isNotEmpty(property) && StringUtils.isNotEmpty(keyword)) {
			String propertyString = "";
			if (property.contains(".")) {
				String propertyPrefix = StringUtils.substringBefore(property, ".");
				String propertySuffix = StringUtils.substringAfter(property, ".");
				criteria.createAlias(propertyPrefix, "model");
				propertyString = "model." + propertySuffix;
			} else {
				propertyString = property;
			}
			// 增加支持精确查询[$$XXX$$]
			if (keyword.startsWith("$$") && keyword.endsWith("$$")) {
				keyword = keyword.substring(2, keyword.length() - 2);
				// System.out.println("propertyString=" + propertyString +
				// "  keyword=" + keyword);
				criteria.add(Restrictions.eq(propertyString, keyword.trim()));
			} else {
				criteria.add(Restrictions.like(propertyString, "%" + keyword + "%"));
			}
		}

	}

	/**
	 * addCriteriaMap
	 * 
	 * @Title: addCriteriaMap
	 * @param propertyMap
	 */
	public void addCriteriaMap() {
		if (propertyMap.size() > 0) {
			Iterator<Entry<String, Object>> itor = propertyMap.entrySet().iterator();
			while (itor.hasNext()) {
				Entry<String, Object> e = itor.next();

				String prop_name = e.getKey();
				Object prop_keyword = e.getValue();
				if (StringUtils.isNotEmpty(prop_name) && prop_keyword != null) {

					String propertyString = "";
					if (prop_name.contains(".")) {
						String propertyPrefix = StringUtils.substringBefore(prop_name, ".");
						String propertySuffix = StringUtils.substringAfter(prop_name, ".");
						criteria.createAlias(propertyPrefix, "model");
						propertyString = "model." + propertySuffix;
					} else {
						propertyString = prop_name;
					}
					criteria.add(Restrictions.like(propertyString, "%" + prop_keyword + "%"));
				}
			}
		}
	}

	public Criteria getCriteria() {
		return criteria;
	}

	public void setCriteria(Criteria criteria) {
		this.criteria = criteria;
	}

	public String getProperty() {
		return property;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public String getQueryString() {
		return queryString;
	}

	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}

	public Map<String, Object> getPropertyMap() {
		return propertyMap;
	}

	public void setPropertyMap(Map<String, Object> propertyMap) {
		this.propertyMap = propertyMap;
	}

	public Map<String, OrderType> getSortMap() {
		return sortMap;
	}

	public void setSortMap(Map<String, OrderType> sortMap) {
		this.sortMap = sortMap;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public QueryStringType getQueryStringType() {
		return queryStringType;
	}

	public void setCriteria(boolean isCriteria) {
		this.isCriteria = isCriteria;
	}

	public boolean isCriteria() {
		return isCriteria;
	}

}
